const http = require('http');
const querystring = require('querystring');

// HTTP请求工具函数
function makeRequest(options, postData = null) {
  return new Promise((resolve, reject) => {
    const req = http.request(options, (res) => {
      let data = '';
      res.on('data', (chunk) => {
        data += chunk;
      });
      res.on('end', () => {
        try {
          const jsonData = JSON.parse(data);
          resolve({ status: res.statusCode, data: jsonData });
        } catch (e) {
          resolve({ status: res.statusCode, data: data });
        }
      });
    });

    req.on('error', (err) => {
      reject(err);
    });

    if (postData) {
      req.write(postData);
    }
    req.end();
  });
}

// 测试智能推荐接口
async function testSmartRecommendation() {
  try {
    console.log('🧪 开始测试智能推荐接口...');

    // 测试参数
    const testParams = {
      city: '北京',
      occasion: '上班',
      includeWeather: 'true',
      useAI: 'true'
    };

    console.log('📋 测试参数:', testParams);

    const queryString = querystring.stringify(testParams);
    const options = {
      hostname: 'localhost',
      port: 7001,
      path: `/api/v1/recommendation/smart?${queryString}`,
      method: 'GET',
      headers: {
        'Authorization': 'Bearer test-token'
      }
    };

    // 发送请求
    const response = await makeRequest(options);

    console.log('✅ 请求成功!');
    console.log('📊 响应状态:', response.status);
    console.log('📄 响应数据:', JSON.stringify(response.data, null, 2));
    
    // 分析响应
    const data = response.data.data;
    if (data && data.recommendations) {
      console.log(`\n🎯 推荐结果分析:`);
      console.log(`• 推荐数量: ${data.recommendations.length}`);
      console.log(`• 数据源: ${data.source || '未知'}`);
      console.log(`• 请求ID: ${data.requestId || '无'}`);
      
      data.recommendations.forEach((rec, index) => {
        console.log(`\n搭配 ${index + 1}:`);
        console.log(`  • 策略: ${rec.strategy || '未知'}`);
        console.log(`  • 评分: ${rec.score || 0}`);
        console.log(`  • 单品数量: ${rec.items?.length || 0}`);
        console.log(`  • 理由: ${rec.reason || '无'}`);
        console.log(`  • 标签: ${rec.tags?.join(', ') || '无'}`);
      });
    }
    
  } catch (error) {
    console.error('❌ 测试失败:', error.message);
    if (error.response) {
      console.error('📄 错误响应:', error.response.data);
    }
  }
}

// 测试AI风格分析接口
async function testAIStyleAnalysis() {
  try {
    console.log('\n🎨 开始测试AI风格分析接口...');

    const options = {
      hostname: 'localhost',
      port: 7001,
      path: '/api/v1/recommendation/ai-style-analysis',
      method: 'GET',
      headers: {
        'Authorization': 'Bearer test-token'
      }
    };

    const response = await makeRequest(options);

    console.log('✅ AI风格分析请求成功!');
    console.log('📊 响应状态:', response.status);
    console.log('📄 响应数据:', JSON.stringify(response.data, null, 2));

  } catch (error) {
    console.error('❌ AI风格分析测试失败:', error.message);
  }
}

// 运行测试
async function runTests() {
  console.log('🚀 开始智能推荐系统测试\n');
  
  // 等待服务启动
  console.log('⏳ 等待服务启动...');
  await new Promise(resolve => setTimeout(resolve, 3000));
  
  await testSmartRecommendation();
  await testAIStyleAnalysis();
  
  console.log('\n✨ 测试完成!');
}

runTests().catch(console.error);
